const express = require('express')
// md5加密
const md5 = require('md5')
// 连接mysql
const query = require('../db');
// console.log(query)
const app = express.Router();

const jwt = require("jsonwebtoken")

const secret = 'abwidbwaodnapn@32t514'

app.post('/login', async (req, res) => {
	let {
		username,
		password
	} = req.body
	password = md5(password)
	const sql = `select * from login where username='${username}' and password='${password}'`
	const data = await query(sql)
	console.log(data)
	if (data.length == 0) {
		res.send({
			msg: '账号或密码错误',
			code: 401,
			data: {
				token: ''
			}
		})
	} else {
		res.send({
			msg: 'ok',
			code: 0,
			data: {
				token: jwt.sign({
					userid: data[0].id,
					name: data[0].username
				}, secret), 
				'username':username 
			}
		})

	}
})

module.exports = app
